/**
 * @Title: CameraPhotoCondSetter.java
 * @Package com.sinotn.ks.node.camera.dao.jdbc.setter
 * @Description: TODO
 * Copyright: Copyright (c) 2015
 * Company:北京信诺软通
 */
package com.sinotn.ks.node.camera.dao.jdbc.setter;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.PreparedStatementSetter;

import com.sinotn.ks.node.camera.dao.cond.CameraPhotoCond;
import com.sinotn.spring.jdbc.PageQuery;

/**
 * @Title CameraPhotoCondSetter.java
 * @Package com.sinotn.ks.node.camera.dao.jdbc.setter
 * @Description Copyright: Copyright (c) 2015 Company:北京信诺软通
 * 
 * @author <a href="mailto:liyunfeng@sinotn.com">李云峰</a>
 * @date 2017年6月23日 下午6:39:07
 * @version V1.0
 */
public class CameraPhotoCondSetter implements PreparedStatementSetter {
	private CameraPhotoCond cond;
	private PageQuery pageQuery;

	public CameraPhotoCondSetter(CameraPhotoCond cond) {
		this.cond = cond;
	}

	public void setPageQuery(PageQuery pageQuery) {
		this.pageQuery = pageQuery;
	}

	@Override
	public void setValues(PreparedStatement ps) throws SQLException {
		if (cond != null) {
			int idx = 1;
			if (!StringUtils.isEmpty(cond.getExamineeName())) {
				ps.setString(idx++, "%" + cond.getExamineeName() + "%");
			}
			if (this.cond.getRoomIdSets() != null) {
				for (Iterator<String> it = this.cond.getRoomIdSets().iterator(); it.hasNext();) {
					ps.setString(idx++, it.next());
				}
			}
			if (cond.getState() != null) {
				ps.setByte(idx++, cond.getState());
			}
			if (cond.getFixed() != null) {
				ps.setByte(idx++, cond.getFixed());
			}
			if (cond.getPhase() != null) {
				ps.setByte(idx++, cond.getPhase());
			}
			if (cond.getExamineeId() != null) {
				ps.setString(idx++, cond.getExamineeId());
			}
		}
		if (this.pageQuery != null) {
			this.pageQuery.setValues(ps);
		}
	}

	public String getCameraPhotoCondSql() {
		if (this.cond == null) {
			return "";
		}
		StringBuilder sb = new StringBuilder();

		// 这里需要注意，要与上面的查询条件顺序相匹配，否则查询的记录不对
		if (StringUtils.isNotBlank(this.cond.getExamineeName())) {
			if (sb.length() == 0) {
				sb.append(" WHERE ");
			} else {
				sb.append(" AND ");
			}
			sb.append("EXAMINEE_NAME LIKE ?");
		}

		if (this.cond.getRoomIdSets() != null) {
			if (sb.length() == 0) {
				sb.append(" WHERE ");
			} else {
				sb.append(" AND ");
			}
			sb.append("(");
			int size = this.cond.getRoomIdSets().size();
			for (int i = 0; i < size; i++) {
				if (i > 0) {
					sb.append(" OR ");
				}
				sb.append("ROOM_ID=?");
			}
			sb.append(")");
		}

		if (this.cond.getState() != null) {
			if (sb.length() == 0) {
				sb.append(" WHERE ");
			} else {
				sb.append(" AND ");
			}
			sb.append("STATE=?");
		}
		
		if (this.cond.getFixed() != null) {
			if (sb.length() == 0) {
				sb.append(" WHERE ");
			} else {
				sb.append(" AND ");
			}
			sb.append("FIXED=?");
		}
		
		if (this.cond.getPhase() != null) {
			if (sb.length() == 0) {
				sb.append(" WHERE ");
			} else {
				sb.append(" AND ");
			}
			sb.append("PHASE = ? ");
		}
		
		if (this.cond.isNormal()) {
			if (sb.length() == 0) {
				sb.append(" WHERE ");
			} else {
				sb.append(" AND ");
			}
			sb.append("PHASE in('0', '2') ");
		}
		
		if (this.cond.getExamineeId() != null) {
			if (sb.length() == 0) {
				sb.append(" WHERE ");
			} else {
				sb.append(" AND ");
			}
			sb.append("EXAMINEE_ID=?");
		}

		return sb.toString();
	}

}
